﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ACPSController acControl;
        Load resistor;
        Multimeter dmm;
        Wiring wire;

        public MainWindow()
        {
            InitializeComponent();
            acControl = new ACPSController();
            resistor = new Load();
            dmm = new Multimeter();
            wire = new Wiring();
            Reset();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            double input;
            try
            {
                input = Convert.ToDouble(voltageTextBox.Text);
                acControl.SetVoltage(input);
                voltageSlider.Value = input;  //can be removed if notifypropertychange
                DisplayMeasurements();
            }
            catch (FormatException)
            {
                voltageTextBox.Text = "Invalid";
            }
            catch (ArgumentOutOfRangeException)
            {
                voltageTextBox.Text = "Invalid";
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            double input;
            try
            {
                input = Convert.ToDouble(currentTextBox.Text);
                acControl.SetCurrent(input);
                currentSlider.Value = input;  //can be removed due if notifypropertychange
                DisplayMeasurements();
            }
            catch (FormatException)
            {
                currentTextBox.Text = "Invalid";
            }
            catch (ArgumentOutOfRangeException)
            {
                currentTextBox.Text = "Invalid";
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            acControl.SetVoltage(voltageSlider.Value);
            voltageSetLabel.Content = Math.Round(acControl.Voltage, 2);
            currentSetLabel.Content = Math.Round(acControl.Current, 2);
            currentSlider.SelectionEnd = acControl.SetCurrentAllowed();
            DisplayMeasurements();
        }

        private void slider2_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            acControl.SetCurrent(currentSlider.Value);
            currentSetLabel.Content = Math.Round(acControl.Current, 2);
            DisplayMeasurements();
        }

        private void slider3_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //try
            //{
                resistor.Resistance = resistanceSlider.Value;
                DisplayMeasurements();
            //}
            //catch (ArgumentOutOfRangeException)
            //{
              //  resistanceSlider.Value = 1;
            //}
        }

        private void DisplayMeasurements()
        {
            wire.Voltage = dmm.ReadVoltage(resistor, acControl);
            //wire.Voltage = 141;
            wire.Current = dmm.ReadCurrent(resistor, acControl);
            voltReadLabel.Content = wire.Voltage + " Volts";
            currReadLabel.Content = wire.Current + " Amps";
            modeLabel.Content = "Mode: " + acControl.ReadMode(resistor).ToString();
            voltageTextBox.Text = "";
            currentTextBox.Text = "";
            wire.SetGauge();
            wire.SetLength();
        }
        
        public void Reset()
        {
            acControl.Voltage = 0;
            acControl.Current = 0;
            resistor.Resistance = 200;
            this.DataContext = wire;
            voltageSlider.Value = acControl.Voltage;
            voltageSlider.Maximum = SignalConstants.voltageMax;
            voltageSlider.SelectionEnd = SignalConstants.voltageMax;
            currentSlider.Value = acControl.Current;
            currentSlider.Maximum = SignalConstants.currentMax;
            currentSlider.SelectionEnd = SignalConstants.currentMax;
            resistanceSlider.Value = resistor.Resistance;
            resistanceSlider.Minimum = resistor.ResMin;
            modeLabel.Content = "Mode: ";
            button4.Content = "Calibrate";
            button4.Foreground = Brushes.Black;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (button4.Content.Equals("EXIT CAL"))
            {
                Reset();
            }
            else
            {
                SecureCalibration secureCal = new SecureCalibration();
                if (!secureCal.checkWindowsUser())
                {
                    MessageBox.Show("Need Admin Privilege");
                }
                else
                {
                    var login = new LoginWindow(this);
                    login.Show();
                }
            }
        }
    }
}
